#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>

// #define SDA PB11
// #define SCL PB10
// #define CS PB1
// #define RST PB0

void delay_ms(int val){
	uint16_t i;
	for(i=0;i<val;i++){
		TCNT1 = 0;
		while(TCNT1<1000);
	}
}

void SDA(uint8_t val){
  if(val){
  	PORTB|=_BV(0);
  }else{
	PORTB&=~_BV(0);
  }
}

void SCL(uint8_t val){
  if(val){
  	PORTA|=_BV(0);
  }else{
	PORTA&=~_BV(0);
  }
}

void CS(uint8_t val){
  if(val){
  	PORTB|=_BV(1);
  }else{
	PORTB&=~_BV(1);
  }
}

void RST(uint8_t val){
  if(val){
  	PORTA|=_BV(1);
  }else{
	PORTA&=~_BV(1);
  }
}

PROGMEM prog_uint8_t lcdinit[] = {
	0xFF, 0x30, 0xFF, 0x52, 0xFF, 0x01, 0xE3, 0x00, 0x03, 0x40,
	0x04, 0x00, 0x05, 0x03, 0xF6, 0xC0, 0xF0, 0x00, 0x0A, 0x01,
	0x23, 0xA2, 0x24, 0x10, 0x25, 0x0A, 0x26, 0x2E, 0x27, 0x2E,
	0x38, 0x9C, 0x39, 0xA7, 0x3A, 0x67, 0x49, 0x3C, 0x6D, 0x00,
	0x6E, 0x00, 0x91, 0x67, 0x92, 0x67, 0x99, 0x54, 0x9B, 0x56,
	0xA0, 0x55, 0xA1, 0x50, 0xA4, 0x9C, 0xA7, 0x02, 0xA8, 0x01,
	0xA9, 0x01, 0xAA, 0xFC, 0xAB, 0x28, 0xAC, 0x06, 0xAD, 0x06,
	0xAE, 0x06, 0xAF, 0x03, 0xB0, 0x08, 0xB1, 0x26, 0xB2, 0x28,
	0xB3, 0x28, 0xB4, 0x03, 0xB5, 0x08, 0xB6, 0x26, 0xB7, 0x08,
	0xB8, 0x26, 0xFF, 0x30, 0xFF, 0x52, 0xFF, 0x02, 0xB0, 0x03,
	0xB1, 0x06, 0xB2, 0x04, 0xB3, 0x26, 0xB4, 0x2D, 0xB5, 0x2D,
	0xB6, 0x09, 0xB7, 0x2D, 0xB8, 0x08, 0xB9, 0x02, 0xBA, 0x10,
	0xBB, 0x0F, 0xBC, 0x10, 0xBD, 0x14, 0xBE, 0x19, 0xBF, 0x11,
	0xC0, 0x18, 0xC1, 0x08, 0xD0, 0x07, 0xD1, 0x0C, 0xD2, 0x06,
	0xD3, 0x2C, 0xD4, 0x2D, 0xD5, 0x33, 0xD6, 0x0F, 0xD7, 0x32,
	0xD8, 0x0B, 0xD9, 0x02, 0xDA, 0x10, 0xDB, 0x0F, 0xDC, 0x11,
	0xDD, 0x13, 0xDE, 0x19, 0xDF, 0x11, 0xE0, 0x19, 0xE1, 0x07,
	0xFF, 0x30, 0xFF, 0x52, 0xFF, 0x03, 0x05, 0x00, 0x06, 0x00,
	0x07, 0x02, 0x08, 0x01, 0x09, 0x02, 0x0A, 0x03, 0x0B, 0x04,
	0x70, 0x0F, 0x71, 0xC0, 0x34, 0xB1, 0x35, 0x77, 0x36, 0x20,
	0x37, 0x13, 0x40, 0x03, 0x41, 0x04, 0x42, 0x05, 0x43, 0x06,
	0x45, 0x08, 0x46, 0x09, 0x48, 0x0A, 0x49, 0x0B, 0x50, 0x07,
	0x51, 0x08, 0x52, 0x09, 0x53, 0x0A, 0x55, 0x0C, 0x56, 0x0D,
	0x58, 0x0E, 0x59, 0x0F, 0x80, 0x1F, 0x81, 0x00, 0x82, 0x15,
	0x83, 0x16, 0x84, 0x0E, 0x85, 0x10, 0x86, 0x0A, 0x87, 0x0C,
	0x88, 0x02, 0x8A, 0x04, 0x96, 0x1F, 0x97, 0x00, 0x98, 0x17,
	0x99, 0x18, 0x9A, 0x0D, 0x9B, 0x0F, 0x9C, 0x09, 0x9D, 0x0B,
	0x9E, 0x01, 0xA0, 0x03, 0xB0, 0x00, 0xB1, 0x1F, 0xB2, 0x15,
	0xB3, 0x16, 0xB4, 0x0B, 0xB5, 0x09, 0xB6, 0x0F, 0xB7, 0x0D,
	0xB8, 0x03, 0xBA, 0x01, 0xC6, 0x00, 0xC7, 0x1F, 0xC8, 0x17,
	0xC9, 0x18, 0xCA, 0x0C, 0xCB, 0x0A, 0xCC, 0x10, 0xCD, 0x0E,
	0xCE, 0x04, 0xD0, 0x02, 0xFF, 0x30, 0xFF, 0x52, 0xFF, 0x00,
	0x36, 0x0A, 0x11, 0x00, //0x29,0x00,
	//0X100,0X001,0X100,0X028,0X100,0X010,0X0FF,0X130,0X0FF,0X152,
};

  
void delay(uint16_t n){
	uint16_t i;
	for(i=0;i<n;i++){
		TCNT1 = 0;
		while(TCNT1<10000);
	}
}


void SPIwritecmd(unsigned char cmd) {
	unsigned char a, b;
	b = cmd;
	SCL(0);
	SDA(0);
	SCL(1);
	for (a = 0; a < 8; a++) {
		SCL(0);
		if (b & 0x80) {
			SDA(1);
		} else {
			SDA(0);
		}
		SCL(1);
		b <<= 1;
	}
}
void SPIwritedat(unsigned char dat) {
	unsigned char a;
	SCL(0);
	SDA(1);
	SCL(1);
	for (a = 0; a < 8; a++) {
		SCL(0);
		if (dat & 0x80) {
			SDA(1);
		} else {
			SDA(0);
		}
		SCL(1);
		dat <<= 1;
	}
}

void LCDinit3() {
	unsigned int a;
	uint8_t val;
  	RST(0);
	delay_ms(200);
  	RST(1);
	delay_ms(200);
	for (a = 0; a < 162; a++) {
		CS(0);
		val	= pgm_read_byte_near(lcdinit + a * 2);
		SPIwritecmd(val);
		val	= pgm_read_byte_near(lcdinit + a * 2 + 1);
		SPIwritedat(val);
		CS(1);
	}
	delay_ms(200);
	CS(0);
	SPIwritecmd(0x29);
	SPIwritedat(0x00);
	CS(1);

}


int main(void) {
  TCCR1B = 1;

  DDRB |= _BV(0);
  DDRB |= _BV(1);
  DDRB |= _BV(3);
  
  DDRA |= _BV(0);
  DDRA |= _BV(1);
  
  PORTB|=_BV(3);
  LCDinit3();

  for(;;) 
  {
    delay(100);
	PORTB&=~_BV(3);
    delay(100);
	PORTB|=_BV(3);
  }
}
